﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReconocedorSonidosVocales
{

    class ReadFile
    {

        public StreamReader reader;
        public String name;

        public ReadFile(String fileName)
        {

            reader = new StreamReader(fileName);
            this.name = System.IO.Path.GetFileName(fileName.Substring(0,fileName.Length-4));
        }
        public List<Muestra> leerMuestras(String tipo)
        {
            System.Globalization.CultureInfo culturaDelSistema = System.Threading.Thread.CurrentThread.CurrentCulture;
            String line = "";
            String[] values = null;
            Double[] vectorDeLaMuestra = null;
            int maxLimit = 500;
            Double[] vectorDeLaMuestraFixedSize = new Double[maxLimit];

            String vocal="";
            List<Muestra> muestras= new List<Muestra>();
            int minimo = 999999;
          //  int max = 0;
            int promedio = 0;
            List<Double> sizes = new List<Double>();

            while ((line = reader.ReadLine()) != null)
            {
                
                values = line.Split(' ');
                if (values.Count() < maxLimit || name.Equals("lpc"))
                {
                    vectorDeLaMuestra = new Double[values.Count() - 1];//-1, porque último dato es la vocal asociada con el vector
                    for (int i = 0; i < vectorDeLaMuestra.Count(); i++)
                    {
                       if (culturaDelSistema.NumberFormat.CurrencyDecimalSeparator.ToString().Equals(","))//los archivos usan punto para separar decimales, porque sistema puede usar , para separar decimales como mi compu
                       {
                            values[i]=values[i].Replace('.',',');
                       }
                      
                       if (Math.Abs(Double.Parse(values[i])) < 1 || Double.Parse(values[i]) == 0)
                       {
                           
                           vectorDeLaMuestra[i] = Math.Abs(Double.Parse(values[i]));
                       }
                       else
                       {
                           vectorDeLaMuestra[i] = Math.Abs(1 / Double.Parse(values[i]));                           
                       }
                    }
                    Double maximo = vectorDeLaMuestra.Max();
                    for (int k = 0; k < vectorDeLaMuestra.Count(); k++)
                    {
                        vectorDeLaMuestra[k] = vectorDeLaMuestra[k] / maximo;
                    }
                    if (!name.Equals("lpc") && !name.Equals("cepstrum"))
                    {
                        int mitad=vectorDeLaMuestra.Count()/2;
                        for (int j = 0; j < mitad; j++)
                        {
                            vectorDeLaMuestraFixedSize[(maxLimit / 2) + j] = vectorDeLaMuestra[mitad + j];
                        }
                        
                        for (int j = ((maxLimit/2)-1); j > (maxLimit / 2) - mitad; j--)
                        {
                            vectorDeLaMuestraFixedSize[j] = vectorDeLaMuestra[mitad + (j - ((maxLimit / 2) - 1)) - 1];
                        }
                    }

                    Double maximo2 = vectorDeLaMuestraFixedSize.Max();
                    for (int k = 0; k < vectorDeLaMuestraFixedSize.Count(); k++)
                    {
                        vectorDeLaMuestraFixedSize[k] = vectorDeLaMuestraFixedSize[k] / maximo2;
                    }


                    /*for (int j = 0; j < min(maxLimit, vectorDeLaMuestra.Count()); j++)
                    {
                        vectorDeLaMuestraFixedSize[j] = vectorDeLaMuestra[j];
                    }*/
                    String ultimaValor = values[values.Count()-1];
                    vocal = values[values.Count()-1].Substring(1, 1);
                

                    /*if (vectorDeLaMuestra.Count()<minimo)
                    {
                        minimo = vectorDeLaMuestra.Count();
                    }
                    if (vectorDeLaMuestra.Count() > max)
                    {
                        max = vectorDeLaMuestra.Count();
                    }
                    sizes.Add(vectorDeLaMuestra.Count());
                    promedio += vectorDeLaMuestra.Count();*/
                    if (tipo.Equals("lpc") || tipo.Equals("cepstrum"))
                    {
                        muestras.Add(new Muestra(vectorDeLaMuestra, vocal, this.name));
                    }
                    else {
                        muestras.Add(new Muestra(vectorDeLaMuestraFixedSize, vocal, this.name));
                    }
                }
            }
           // promedio = promedio / sizes.Count();
            //ful rango 525
            //nuevo 489
            return muestras;
        }

        public int min(int a, int b) {
            if (a < b)
            {
                return a;
            }
            else {
                return b;
            }
        }
        public Weights readWeights()
        {
           // Double[] weights = null;
            String line = "";
            Weights w = new Weights();
            String[] values = null;

            if ((line = reader.ReadLine()) != null)
            {
                values = line.Split(' ');
            }
            else {
                return null;
            }
            w.values = new Double[values.Count()-1];
            for (int i = 0; i < w.values.Count(); i++ )
            {
                w.values[i]=Double.Parse(values[i]);
            }
            w.output = values[w.values.Count()].Substring(1,1);

            return w;
        }
        public int getCategoriesAmount()
        {
            int categoriesCounter = 0;
            while(reader.ReadLine() != null){
                categoriesCounter++;
            }
            this.reader.Close();
            reader = new StreamReader(this.name);
            return categoriesCounter;

        }
        public void close()
        {
            this.reader.Close();
        }

    }
}
